package com.baizhi.cmfzmanager.service.impl;

import com.baizhi.cmfzmanager.dao.UserDao;
import com.baizhi.cmfzmanager.entity.User;
import com.baizhi.cmfzmanager.entity.UserDTO;
import com.baizhi.cmfzmanager.service.UserService;
import com.baizhi.cmfzmanager.util.ThreadUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

@Service
public class UserServiceImpl implements UserService {

    Logger logger= LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;
    @Override
    public Map getUsers(int page, int rows) {
        logger.info("分页查询方法开始");

        logger.warn("分页查询方法开始");
        logger.error("分页查询方法开始");
        int start=(page-1)*rows;
        IPage<User> iPage = new Page<>(start,rows);
        IPage<User> userIPage = userDao.selectPage(iPage, null);
        List<User> userList = userIPage.getRecords();
        long count = userIPage.getTotal();
        Map map = new HashMap();
        map.put("total",count);
        map.put("rows",userList);
        logger.info("分页查询方法结束");
        return map;
    }

    @Override
    public List<UserDTO> getSexData() {
        logger.info("性别信息查询方法开始");
        return userDao.getSexData();
    }

    @Override
    public List<Map> getProvinceData() {
        logger.info("省份信息查询方法开始");
        return userDao.getProvinceData();
    }

    @Override
    public List<UserDTO> getRegistData() {
        logger.info("注册信息查询方法开始");
        return userDao.getRegistData();
    }

    @Override
    public Map getThreeByOne() {
        logger.info("单线程查询方法开始");
        List<UserDTO> sexData = userDao.getSexData();
        List<Map> provinceData = userDao.getProvinceData();
        List<UserDTO> registData = userDao.getRegistData();
        Map map=new HashMap();
        map.put("sex",sexData);
        map.put("province",provinceData);
        map.put("regist",registData);

        return map;
    }

    @Override
    public Map getThreeByMany() throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(3);
        Map map = new HashMap();
        ThreadUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                logger.info("countdownlatch查询性别信息");
                List<UserDTO> sexData = userDao.getSexData();
                map.put("sex",sexData);
                latch.countDown();
            }
        });

        ThreadUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                logger.info("countdownlatch查询省份信息");
                List<Map> provinceData = userDao.getProvinceData();
                map.put("province",provinceData);
                latch.countDown();
            }
        });

        ThreadUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
                logger.info("countdownlatch查询注册信息");
                List<UserDTO> registData = userDao.getRegistData();
                map.put("regist",registData);
                latch.countDown();
            }
        });
        latch.await();

        return map;
    }

    @Override
    public Map getThreeByFuture() throws Exception{
        Map map = new HashMap();

        Future<Boolean> submit1 = ThreadUtil.executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                logger.info("future查询性别信息");
                List<UserDTO> sexData = userDao.getSexData();
                map.put("sex",sexData);
                return true;
            }
        });

        Future<Boolean> submit2 = ThreadUtil.executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                logger.info("future查询省份信息");
                List<Map> provinceData = userDao.getProvinceData();
                map.put("province",provinceData);
                return true;
            }
        });

        Future<Boolean> submit3 = ThreadUtil.executor.submit(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                logger.info("future查询注册信息");
                List<UserDTO> registData = userDao.getRegistData();
                map.put("regist",registData);
                return true;
            }
        });
        if(submit1.get()&&submit2.get()&&submit3.get()){
            return map;
        }

        return map;
    }

    @Override
    public void freeze(int id) {
        logger.info("冻结用户方法");
        User user = new User();
        user.setUserId(id);
        user.setUserStatus(1);

        userDao.updateById(user);
    }

    @Override
    public User login(String userTelephone, String userPassword) {
        return userDao.login(userTelephone,userPassword);
    }

    @Override
    public void updateUsername(Integer userId, String username) {
        userDao.updateUsername(userId,username);
    }

    @Override
    public void updateUserImage(Integer userId, String filename) {
        userDao.updateUserImage(userId,filename);
    }

    @Override
    public void updateNickname(Integer userId, String userNickname) {
        userDao.updateNickname(userId,userNickname);
    }
}
